package com.kfh.ybracelet.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kfh.ybracelet.R;
import com.kfh.ybracelet.base.BaseActivity;
import com.kfh.ybracelet.net.HttpUtil;
import com.kfh.ybracelet.utils.CommonUtils;
import com.kfh.ybracelet.utils.Constant;

/**
 * 忘记密码 修改密码页面
 * <p/>
 * Created by Aaron on 15/10/28.
 */
public class ForgetInputPswActivity extends BaseActivity {

    private EditText et_new_psw;
    private EditText et_new_psw_repeat;
    //type 0 是注册 输入密码  1 是忘记密码 重置密码
    private int type;
    //    private String nickName;
//    private String phone;
    private String token;//

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_input_psw);

//        mApplication.addActivity(this);

//        type = getIntent().getIntExtra("type", 0);

//        phone = getIntent().getStringExtra("phone");
//        nickName = getIntent().getStringExtra("nickName");
        token = getIntent().getStringExtra("token");

        initWidget();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

//        tv_title.setText(getResources().getString(type == 1 ? R.string.reset_psw : R.string.input_psw));
        tv_title.setText(getResources().getString(R.string.input_psw));

        et_new_psw = (EditText) findViewById(R.id.et_new_psw);
        et_new_psw_repeat = (EditText) findViewById(R.id.et_new_psw_repeat);

        Button btn_commit = (Button) findViewById(R.id.btn_commit);

        iv_left.setOnClickListener(this);
        btn_commit.setOnClickListener(this);

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;

            case R.id.btn_commit: //完成

                if (CommonUtils.isNull(et_new_psw.getText().toString().trim())) {
                    showToast("请输入新密码");
                    return;
                }

                if (!et_new_psw.getText().toString().trim().equals(et_new_psw_repeat.getText().toString().trim())) {
                    showToast("您两次输入的新密码不一致");
                    return;
                }

                setPsw();

                break;

        }
    }

    /**
     * 设置密码(注册, 忘记密码)
     */
    private void setPsw() {

        String psw = et_new_psw.getText().toString().trim();

        JSONObject j = new JSONObject();
        j.put("token", token);
        j.put("pwd", CommonUtils.toMd5(psw.getBytes()));

        HttpUtil.setPostRequest("获取中...", this, Constant.WEB_ROOT + Constant.FORGET,
                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            org.json.JSONObject result = new org.json.JSONObject(str);

//                            if (result.getInt("code") == Constant.STATUS_SUCCESS) {

                                //注册成功. 跳转到登陆页面
                                Intent mIntent = new Intent(ForgetInputPswActivity.this, LoginActivity.class);
                                startActivity(mIntent);
                                finish();

//                            } else {
//                                showToast(Constant.showErrorMsg(result.getInt("code"), ForgetInputPswActivity.this));
//                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast("注册失败");
                        }

                    }
                }, null);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mApplication.cancelPendingRequests(getClass().getName());
    }
}
